﻿/*
	Copyright (c) 第三界主 2014
	代码使用MIT协议发布
*/

#include "wayland-hello.h"

static void paint_pixels(void *image, int padding, int width, 
	int height, uint32_t time)
{
	const int halfh = padding + (height - padding * 2) / 2;
	const int halfw = padding + (width - padding * 2) / 2;
	int ir, or;
	uint32_t *pixel = image;
	int y;

	/* squared radii thresholds */
	or = (halfw < halfh ? halfw : halfh) - 8;
	ir = or - 32;
	or *= or;
	ir *= ir;

	pixel += padding * width;
	for (y = padding; y < height - padding; y++) 
	{
		int x;
		int y2 = (y - halfh) * (y - halfh);

		pixel += padding;
		for (x = padding; x < width - padding; x++) 
		{
			uint32_t v;

			/* squared distance from center */
			int r2 = (x - halfw) * (x - halfw) + y2;

			if (r2 < ir)
				v = (r2 / 32 + time / 64) * 0x0080401;
			else if (r2 < or)
				v = (y + time / 32) * 0x0080401;
			else
				v = (x + time / 16) * 0x0080401;
			v &= 0x00ffffff;

			/* cross if compositor uses X from XRGB as alpha */
			if (abs(x - y) > 6 && abs(x + y - height) > 6)
				v |= 0xff000000;

			*pixel++ = v;
		}

		pixel += padding;
	}
}

void PaintProc(HSURFACE hSurface, LPPAINTSTRUCTURE lpPaint, uint32_t uTime)
{
	SIZE sz = wlGetSurfaceSize(hSurface);
	paint_pixels(lpPaint->buffer->data, 20, sz.width, sz.height, uTime);
	//memset(lpBuffer->data, 0xff, width * height * 4);
}

int main(int argc, char **argv)
{
	wlInit();

	int width = 250, height = 250;

	HSURFACE surface = wlCreateSurface("wayland-hello", width, height);

	wlSetPaintProc(surface, PaintProc);

	int ret = 0;
	while (ret != -1)
	{
		ret = wlDispatch();
	}

	wlTerminate();

	return 0;
}

